package com.kkd.kkdapp.activity;

import java.io.File;
import java.io.FileNotFoundException;

import com.kkd.kkdapp.R;
import com.kkd.kkdapp.base.BaseActivity;
import com.kkd.kkdapp.db.CurUserModel;
import com.kkd.kkdapp.listener.OnSingleClickListener;
import com.kkd.kkdapp.utils.AppUtil;
import com.kkd.kkdapp.utils.FileUtils;
import com.kkd.kkdapp.utils.PreferenceUtils;

import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.assist.FailReason;
import com.nostra13.universalimageloader.core.listener.ImageLoadingListener;

import android.content.ContentValues;
import android.content.Intent;
import android.graphics.Bitmap;
import android.media.MediaScannerConnection;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;

/****
 * 我的二维码
 * 
 **/
public class MyQrcodeActivity extends BaseActivity implements OnClickListener {

	private RelativeLayout title_img;
	private ImageView qrcode_img;
	private Button mShareBtn;
	private Button mSaveBtn;
	private ImageView mHeadImg;
	private RelativeLayout mQrcodeFrame;
	private String inviteFriendUrl;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_two_dimension);
		try {
			inviteFriendUrl = PreferenceUtils.getInstance().getInviteFriendUrl(
					this);
		} catch (Exception e) {
			// 没有就没有，忽略掉
		}

		initView();
		setListener();
	}

	public void initView() {
		title_img = (RelativeLayout) this.findViewById(R.id.title_img);
		qrcode_img = (ImageView) this.findViewById(R.id.qrcode_img);
		mQrcodeFrame = (RelativeLayout) findViewById(R.id.qrcode_frame);
		mShareBtn = (Button) findViewById(R.id.share_btn);
		mShareBtn.setOnClickListener(new OnSingleClickListener() {

			@Override
			public void onSingleClick(View v) {
				toShareURL(MyQrcodeActivity.this, inviteFriendUrl,
						"快快贷理财", "快快贷理财活期8%,赶快加入和我一起向钱进.",
						R.drawable.share_logo);
			}
		});
		mSaveBtn = (Button) findViewById(R.id.save_btn);
		mSaveBtn.setOnClickListener(new OnSingleClickListener() {

			@Override
			public void onSingleClick(View v) {

				toSave();

			}
		});
		mHeadImg = (ImageView) findViewById(R.id.head);
		String friendShareUrl = PreferenceUtils.getInstance().getTwoDimension(
				this);
		ImageLoader.getInstance().displayImage(friendShareUrl, qrcode_img);
		ImageLoader.getInstance().displayImage(
				PreferenceUtils.getInstance().getHeadUrl(this), mHeadImg);
	}

	/**
	 * toSave:(这里用一句话描述这个方法的作用). <br/>
	 * 
	 * @author askzhao
	 */
	protected void toSave() {

		if (FileUtils.checkSDCard()) {
			if (FileUtils.isFileUrlExit(FileUtils.getKKDRootPath() + "qrcode/"
					+ CurUserModel.getCurUser().customerId + "_qrcode.png")) {
				AppUtil.makeToastForCustomer(this, "二维码图片已经保存过,无需多次保存", false);
			} else {
				String path = FileUtils.saveToSdCard(mQrcodeFrame, false);
				// 通知相册刷新
				FileUtils.tellMedia2Update(MyQrcodeActivity.this, path);
				AppUtil.makeToastForCustomer(this, "二维码图片已经成功保存到:" + path
						+ "目录下", false);
			}

		} else {
			showToast("未检测到可用sd卡,无法使用本功能");
		}

	}

	public void setListener() {
		title_img.setOnClickListener(this);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.title_img:
			finish();
			break;
		}
	}
}
